package com.jerry.walking.module.activityPath.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.walking.R;
import com.jerry.walking.base.BaseActivity;
import com.jerry.walking.base.BaseApplication;
import com.jerry.walking.base.BaseViewHolder;
import com.jerry.walking.base.MyBaseAdapter;
import com.jerry.walking.constant.Urls;
import com.jerry.walking.module.activityPath.bean.SpecialFirst1;
import com.jerry.walking.module.activityPath.fragment.SpecialFragment;
import com.jerry.walking.utils.OkHttpUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yang on 2016/5/13.
 *
 *          路线专题中的点击事件的显示
 *
 */
public class SpecialFirst extends BaseActivity {

    @BindView(R.id.textview4)
    TextView textView;

    @BindView(R.id.lv_path_special_first)
    PullToRefreshListView listView;

    @BindView(R.id.iv_path_search1)
    ImageView imageView;

    private List<SpecialFirst1.TrailsBean> items;

    private MyBaseAdapter<SpecialFirst1.TrailsBean> adapter;

    //传递资源
    private int page = 1;
    private int id = -1;
    private String name;

    private ImageLoader imageLoader;

    @Override
    protected int getLayoutId() {

        id = Integer.parseInt(getIntent().getStringExtra("id"));
        name = getIntent().getStringExtra("name");

        return R.layout.activity_path_special_first;
    }

    @Override
    protected void initViews() {
        super.initViews();

        imageLoader = BaseApplication.getApplication().getImageLoader();

        textView.setText(name);

        listView.setMode(PullToRefreshBase.Mode.BOTH);

        items = new ArrayList<>();

        adapter = new MyBaseAdapter<SpecialFirst1.TrailsBean>(this, items) {
            @Override
            public int getLayoutId() {
                return R.layout.adapter_path_special_first;
            }

            @Override
            public BaseViewHolder getViewHolder() {
                return new ViewHolder();
            }

            @Override
            public void bindViews(View view, BaseViewHolder viewHolder) {
                ViewHolder holder = (ViewHolder) viewHolder;
                DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .cacheOnDisk(true)
                        .cacheInMemory(true)
                        .imageScaleType(ImageScaleType.EXACTLY)
                        .build();
                imageLoader.displayImage(items.get(holder.position).getCover(), holder.imageView, options);

                holder.textView.setText(items.get(holder.position).getName());
                holder.textView1.setText(items.get(holder.position).getDestination());

            }
        };
        listView.setAdapter(adapter);

        //上拉下拉
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                items.clear();
                listView.setMode(PullToRefreshBase.Mode.BOTH);
                page = 1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                loadData();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SpecialFirst.this, SpecialSecond.class);
                intent.putExtra("id", items.get(position-1).getId());
                intent.putExtra("name", items.get(position-1).getName());
                SpecialFirst.this.startActivity(intent);
            }
        });

        //搜索界面跳转
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SpecialFirst.this, SearchActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        super.loadData();

        OkHttpUtils.get(Urls.PATH_ROUTEBASE_ITEM + id + Urls.PATH_ROUTEBASE_ITEM_CURRENT + page + Urls.PATH_ROUTEBASE_ITEM_END + id, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                Gson gson = new Gson();

                SpecialFirst1 specialFirst1 = gson.fromJson(data, SpecialFirst1.class);

                List<SpecialFirst1.TrailsBean> trails = specialFirst1.getTrails();

                if (specialFirst1.getCur_page() == specialFirst1.getTotal_pages()) {
                    //最后一页
                    listView.onRefreshComplete();
                    listView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                }

                items.addAll(trails);
                adapter.notifyDataSetChanged();
                listView.onRefreshComplete();
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });

    }

    class ViewHolder extends BaseViewHolder {
        @BindView(R.id.iv_path_special_first_adapter)
        ImageView imageView;

        @BindView(R.id.tv_path_special_first_adapter)
        TextView textView;

        @BindView(R.id.tv_path_special_first_adapter1)
        TextView textView1;
    }

    /**
     * 回退键
     * @param view
     */
    public void onBack(View view) {
        finish();
    }
}
